﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;
using VoxelBlock.VoxelBlock3D;

namespace VoxelBlock
{
    /// <summary>
    /// 数组转录算法
    /// </summary>
    public class ArrayTranscriptionAlgorithm
    {

        public static T[] MergeArray<T>(List<T[][]> arrayss)
        {
            int maxLength = 0;
            for (int i = 0, loopLength = arrayss.Count; i < loopLength; i++)
            {
                foreach (T[] item in arrayss[i])
                {
                    maxLength += item.Length;
                }
            }
            T[] returnArray = new T[maxLength];

            int loopIndex = 0;
            for (int j = 0, lengthss = arrayss.Count; j < lengthss; j++)
            {
                T[][] arrays = arrayss[j];
                for (int i = 0, loopLength = arrays.Length; i < loopLength; i++)
                {
                    Array.Copy(arrays[i], 0, returnArray, loopIndex, arrays[i].Length);
                    loopIndex += arrays[i].Length;
                }
            }

            return returnArray;
        }


        public static T[] MergeArrays<T>(T[][] arrays)
        {
            int maxLength = 0;
            foreach (T[] item in arrays)
            {
                maxLength += item.Length;
            }
            T[] returnArray = new T[maxLength];

            int loopIndex = 0;
            for (int i = 0, loopLength = arrays.Length; i < loopLength; i++)
            {
                Array.Copy(arrays[i], 0, returnArray, loopIndex, arrays[i].Length);
                loopIndex += arrays[i].Length;
            }
            return returnArray;
        }

        public static T[] MergeArray<T>(params T[][] arrays)
        {
            int maxLength = 0;
            foreach (T[] item in arrays)
            {
                maxLength += item.Length;
            }
            T[] returnArray = new T[maxLength];

            int loopIndex = 0;
            for (int i = 0, loopLength = arrays.Length; i < loopLength; i++)
            {
                Array.Copy(arrays[i], 0, returnArray, loopIndex, arrays[i].Length);
                loopIndex += arrays[i].Length;
            }
            return returnArray;
        }

        [Obsolete]
        public static void TranscriptionBolcksArrayOld
            (
                       ref int[] a_ChunkArray
                     , ref int[] a_VerticesIndex
                     , IList<int> a_AddIndexs
                     , IList<int> a_RemoveIndexs
                     , ref Vector3[] a_VerticesDatas
                     , ref Vector2[] a_UVsDatas
                     , ref Color[] a_ColorsDatas
                     , IList<Vector3[]> a_AddDatas_vertices
                     , IList<Vector2[]> a_AddDatas_UVs
                     , IList<Color[]> a_AddDatas_Colors
              )
        {
            //Test
            //int[] a_ChunkArray = new int[a_ChunksArray.Length];
            //a_ChunksArray.CopyTo(a_ChunkArray, 0);
            //Test Over

            int[] fm_verticesIndex = new int[a_VerticesIndex.Length];
            a_VerticesIndex.CopyTo(fm_verticesIndex, 0);

            //添加的比删除的多
            bool fm_isAddFill;
            //交集
            int[] fm_intersection;

            //差集
            int[] fm_Difference;
            //T[] fm_Difference_Data;


            if (a_AddIndexs.Count >= a_RemoveIndexs.Count)
            {
                fm_isAddFill = true;
                fm_intersection = a_RemoveIndexs.ToArray();

                fm_Difference = new int[a_AddIndexs.Count - a_RemoveIndexs.Count];
                Array.Copy(a_AddIndexs.ToArray(), a_RemoveIndexs.Count, fm_Difference, 0, fm_Difference.Length);

            }
            else
            {
                fm_isAddFill = false;
                fm_intersection = new int[a_AddIndexs.Count];
                Array.Copy(a_RemoveIndexs.ToArray(), fm_intersection, a_AddIndexs.Count);

                fm_Difference = new int[a_RemoveIndexs.Count - a_AddIndexs.Count];
                Array.Copy(a_RemoveIndexs.ToArray(), a_AddIndexs.Count, fm_Difference, 0, fm_Difference.Length);

            }

            int[] return_IndexArray_new = new int[a_VerticesIndex.Length + a_AddIndexs.Count - a_RemoveIndexs.Count];

            int newArrayLength = return_IndexArray_new.Length * VoxelCategories.UNIT_VERTICES_LENGTH;
            Vector3[] returnNewArrayVertices = new Vector3[newArrayLength];
            Vector2[] returnNewArrayUVs = new Vector2[newArrayLength];
            Color[] returnNewArrayColors = new Color[newArrayLength];

            //替换
            for (int i = 0, loop_length = fm_intersection.Length; i < loop_length; i++)
            {
                var loop_Index = a_ChunkArray[fm_intersection[i]];

                fm_verticesIndex[loop_Index] = a_AddIndexs[i];

                a_ChunkArray[a_AddIndexs[i]] = a_ChunkArray[fm_intersection[i]];
                a_ChunkArray[fm_intersection[i]] = -1;


                a_VerticesDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 0] = a_AddDatas_vertices[i][0];
                a_VerticesDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 1] = a_AddDatas_vertices[i][1];
                a_VerticesDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 2] = a_AddDatas_vertices[i][2];
                a_VerticesDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 3] = a_AddDatas_vertices[i][3];


                a_UVsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 0] = a_AddDatas_UVs[i][0];
                a_UVsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 1] = a_AddDatas_UVs[i][1];
                a_UVsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 2] = a_AddDatas_UVs[i][2];
                a_UVsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 3] = a_AddDatas_UVs[i][3];


                a_ColorsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 0] = a_AddDatas_Colors[i][0];
                a_ColorsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 1] = a_AddDatas_Colors[i][1];
                a_ColorsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 2] = a_AddDatas_Colors[i][2];
                a_ColorsDatas[loop_Index * VoxelCategories.UNIT_VERTICES_LENGTH + 3] = a_AddDatas_Colors[i][3];



            }

            if (fm_isAddFill)
            {
                fm_verticesIndex.CopyTo(return_IndexArray_new, 0);
                //添加剩余的
                fm_Difference.CopyTo(return_IndexArray_new, fm_verticesIndex.Length);
                //更新Chunk
                for (int i = 0; i < fm_Difference.Length; i++)
                {
                    a_ChunkArray[fm_Difference[i]] = i + fm_verticesIndex.Length;
                }

                //添加真实数据
                a_VerticesDatas.CopyTo(returnNewArrayVertices, 0);

                a_UVsDatas.CopyTo(returnNewArrayUVs, 0);
                a_ColorsDatas.CopyTo(returnNewArrayColors, 0);

                //添加剩余的
                int loop_StartIndex = fm_intersection.Length;
                for (int i = 0; i < fm_Difference.Length; i++)
                {
                    int indexLength = a_VerticesDatas.Length + i * VoxelCategories.UNIT_VERTICES_LENGTH;
                    returnNewArrayVertices[indexLength + 0] = a_AddDatas_vertices[loop_StartIndex + i][0];
                    returnNewArrayVertices[indexLength + 1] = a_AddDatas_vertices[loop_StartIndex + i][1];
                    returnNewArrayVertices[indexLength + 2] = a_AddDatas_vertices[loop_StartIndex + i][2];
                    returnNewArrayVertices[indexLength + 3] = a_AddDatas_vertices[loop_StartIndex + i][3];


                    returnNewArrayUVs[indexLength + 0] = a_AddDatas_UVs[loop_StartIndex + i][0];
                    returnNewArrayUVs[indexLength + 1] = a_AddDatas_UVs[loop_StartIndex + i][1];
                    returnNewArrayUVs[indexLength + 2] = a_AddDatas_UVs[loop_StartIndex + i][2];
                    returnNewArrayUVs[indexLength + 3] = a_AddDatas_UVs[loop_StartIndex + i][3];

                    returnNewArrayColors[indexLength + 0] = a_AddDatas_Colors[loop_StartIndex + i][0];
                    returnNewArrayColors[indexLength + 1] = a_AddDatas_Colors[loop_StartIndex + i][1];
                    returnNewArrayColors[indexLength + 2] = a_AddDatas_Colors[loop_StartIndex + i][2];
                    returnNewArrayColors[indexLength + 3] = a_AddDatas_Colors[loop_StartIndex + i][3];
                }
            }
            else
            {
                //开始删除多余的数据
                //删除的索引集合
                int[] var_DifferenceVerticesData_Index = new int[fm_Difference.Length * VoxelCategories.UNIT_VERTICES_LENGTH];
                for (int i = 0, loop_length = fm_Difference.Length; i < loop_length; i++)
                {
                    int loop_removeIndex = a_ChunkArray[fm_Difference[i]];

                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 0] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 0;
                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 1] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 1;
                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 2] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 2;
                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 3] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 3;

                }
                returnNewArrayVertices = RemoveArray_simple(a_VerticesDatas, var_DifferenceVerticesData_Index);
                returnNewArrayUVs = RemoveArray_simple(a_UVsDatas, var_DifferenceVerticesData_Index);
                returnNewArrayColors = RemoveArray_simple(a_ColorsDatas, var_DifferenceVerticesData_Index);

                //删除的索引集合
                int[] var_DifferenceVerticesIndex_Index = new int[fm_Difference.Length];
                for (int i = 0, loop_length = fm_Difference.Length; i < loop_length; i++)
                {
                    var_DifferenceVerticesIndex_Index[i] = a_ChunkArray[fm_Difference[i]];
                    a_ChunkArray[fm_Difference[i]] = -1;
                }
                //删除多余的
                return_IndexArray_new = RemoveArray_simple(fm_verticesIndex, var_DifferenceVerticesIndex_Index);



            }
            a_VerticesIndex = return_IndexArray_new;
            a_VerticesDatas = returnNewArrayVertices;
            a_UVsDatas = returnNewArrayUVs;
            a_ColorsDatas = returnNewArrayColors;

            //return Tuple.Create(return_IndexArray_new, return_DataArray_new);
        }

        /*
        public static void Test()
        {
            int a_Direction = 0;
            int[] a_ChunkArray = new int[16];
            int[] a_VerticesIndex = new int[0];
            Vector3[] a_VerticesDatas = new Vector3[0];
            Vector2[] a_UVsDatas = new Vector2[0];
            Color[] a_ColorsDatas = new Color[0];

            Voxel3D[] a_mainChunkVoxel3Ds = new Voxel3D[16];

            List<int> a_AddIndexs = new List<int>();
            List<int> a_RemoveIndexs = new List<int>();

            for (int i = 0; i < 16; i++)
            {
                a_ChunkArray[i] = -1;
            }

            a_mainChunkVoxel3Ds[4] = new Voxel3D(new Vector3Int(), 0);
            a_mainChunkVoxel3Ds[7] = new Voxel3D(new Vector3Int(), 0);
            a_mainChunkVoxel3Ds[8] = new Voxel3D(new Vector3Int(), 0);
            a_mainChunkVoxel3Ds[2] = new Voxel3D(new Vector3Int(), 0);
            a_AddIndexs.Add(4);
            a_AddIndexs.Add(7);
            a_AddIndexs.Add(8);
            a_AddIndexs.Add(2);

            TranscriptionBolcksArray
            (
                      a_Direction
                     , a_ChunkArray
                     , ref a_VerticesIndex
                     , a_AddIndexs
                     , a_RemoveIndexs
                     , ref a_VerticesDatas
                     , ref a_UVsDatas
                     , ref a_ColorsDatas
                     , a_mainChunkVoxel3Ds

              );
            a_AddIndexs = new List<int>();

            a_mainChunkVoxel3Ds[10] = new Voxel3D(new Vector3Int(), 0);
            a_AddIndexs.Add(10);
            a_RemoveIndexs.Add(4);
            a_RemoveIndexs.Add(7);
            TranscriptionBolcksArray
            (
                      a_Direction
                     , a_ChunkArray
                     , ref a_VerticesIndex
                     , a_AddIndexs
                     , a_RemoveIndexs
                     , ref a_VerticesDatas
                     , ref a_UVsDatas
                     , ref a_ColorsDatas
                     , a_mainChunkVoxel3Ds

              );


        }
    //    */

        public static void TranscriptionBolcksArray
            (
                       int a_Direction
                     , int[] a_ChunkArray
                     , ref int[] a_VerticesIndex
                     , IList<int> a_AddIndexs
                     , IList<int> a_RemoveIndexs
                     , ref Vector3[] a_VerticesDatas
                     , ref Vector2[] a_UVsDatas
                     , ref Vector2[] a_UV3sDatas
                     , ref Color[] a_ColorsDatas
                     , Voxel3D[] a_mainChunkVoxel3Ds

              )
        {
            VoxelMetadata3D voxelMetadata3D = VoxelMetadata3D.Instance;
            //int lengthDirectionIndex = a_Direction * DataConfig.CHUNL_LENGTH6;
            Color color = voxelMetadata3D.allDataColors[a_Direction];

            int[] fm_verticesIndex = new int[a_VerticesIndex.Length];
            a_VerticesIndex.CopyTo(fm_verticesIndex, 0);

            //添加的比删除的多
            bool fm_isAddFill;
            //交集
            int[] fm_intersection;

            //差集
            int[] fm_Difference;
            //T[] fm_Difference_Data;


            if (a_AddIndexs.Count >= a_RemoveIndexs.Count)
            {
                fm_isAddFill = true;
                fm_intersection = a_RemoveIndexs.ToArray();

                fm_Difference = new int[a_AddIndexs.Count - a_RemoveIndexs.Count];
                Array.Copy(a_AddIndexs.ToArray(), a_RemoveIndexs.Count, fm_Difference, 0, fm_Difference.Length);

            }
            else
            {
                fm_isAddFill = false;
                fm_intersection = new int[a_AddIndexs.Count];
                Array.Copy(a_RemoveIndexs.ToArray(), fm_intersection, a_AddIndexs.Count);

                fm_Difference = new int[a_RemoveIndexs.Count - a_AddIndexs.Count];
                Array.Copy(a_RemoveIndexs.ToArray(), a_AddIndexs.Count, fm_Difference, 0, fm_Difference.Length);

            }
            int[] return_IndexArray_newz = new int[a_VerticesIndex.Length + a_AddIndexs.Count - a_RemoveIndexs.Count];

            int[] return_IndexArray_new = new int[a_VerticesIndex.Length + a_AddIndexs.Count - a_RemoveIndexs.Count];

            int newArrayLength = return_IndexArray_new.Length * VoxelCategories.UNIT_VERTICES_LENGTH;
            Vector3[] returnNewArrayVertices = new Vector3[newArrayLength];
            Vector2[] returnNewArrayUVs = new Vector2[newArrayLength];
            Vector2[] returnNewArrayUV3s = new Vector2[newArrayLength];
            Color[] returnNewArrayColors = new Color[newArrayLength];

            //替换
            for (int i = 0, loop_length = fm_intersection.Length; i < loop_length; i++)
            {
                //被替换的索引
                var replacedIndex = fm_intersection[i];
                //新添加的索引
                var newIndex = a_AddIndexs[i];

                //顶点指针
                var vertexPointer = a_ChunkArray[replacedIndex];
                fm_verticesIndex[vertexPointer] = newIndex;


                a_ChunkArray[newIndex] = vertexPointer;
                a_ChunkArray[replacedIndex] = -1;

                Array.Copy(voxelMetadata3D.GetVertices(a_Direction, a_mainChunkVoxel3Ds[newIndex].localPosition), 0, a_VerticesDatas, vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH, VoxelCategories.UNIT_VERTICES_LENGTH);
                //Array.Copy(voxelMetadata3D.allDataVertices, lengthDirectionIndex + loop_AddChunkIndex * 4, a_VerticesDatas, loop_Index * 4, 4);
                //a_VerticesDatas[loop_Index * 4 + 0] = voxelMetadata3D.allDataVertices[lengthDirectionIndex + loop_AddChunkIndex * 4 + 0];
                //a_VerticesDatas[loop_Index * 4 + 1] = voxelMetadata3D.allDataVertices[lengthDirectionIndex + loop_AddChunkIndex * 4 + 1];
                //a_VerticesDatas[loop_Index * 4 + 2] = voxelMetadata3D.allDataVertices[lengthDirectionIndex + loop_AddChunkIndex * 4 + 2];
                //a_VerticesDatas[loop_Index * 4 + 3] = voxelMetadata3D.allDataVertices[lengthDirectionIndex + loop_AddChunkIndex * 4 + 3];


                Array.Copy(voxelMetadata3D.allDataUVs, a_mainChunkVoxel3Ds[newIndex].uvKey * VoxelCategories.UNIT_VERTICES_LENGTH, a_UVsDatas, vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH, VoxelCategories.UNIT_VERTICES_LENGTH);
                //a_UVsDatas[loop_Index * 4 + 0] = voxelMetadata3D.allDataUVs[a_mainChunkVoxel3Ds[loop_AddChunkIndex].uvKey * 4 + 0];
                //a_UVsDatas[loop_Index * 4 + 1] = voxelMetadata3D.allDataUVs[a_mainChunkVoxel3Ds[loop_AddChunkIndex].uvKey * 4 + 1];
                //a_UVsDatas[loop_Index * 4 + 2] = voxelMetadata3D.allDataUVs[a_mainChunkVoxel3Ds[loop_AddChunkIndex].uvKey * 4 + 2];
                //a_UVsDatas[loop_Index * 4 + 3] = voxelMetadata3D.allDataUVs[a_mainChunkVoxel3Ds[loop_AddChunkIndex].uvKey * 4 + 3];

                //Array.Copy(
                //     voxelMetadata3D.GetColors(a_Direction, a_mainChunkVoxel3Ds[loop_AddChunkIndex].colorShadow[a_Direction])
                //    , 0, a_ColorsDatas, loop_Index * 4, 4);

                color.a = (a_mainChunkVoxel3Ds[newIndex].selectKey) / 255f;

                //var colorShadow = a_mainChunkVoxel3Ds[newIndex].colorShadow[a_Direction];
                //BasicAlgorithm.SetShadowColor(a_mainChunkVoxel3Ds[newIndex].colorShadows[a_Direction], a_ColorsDatas , vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH, color);
                a_ColorsDatas[vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH + 0] = color;
                a_ColorsDatas[vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH + 1] = color;
                a_ColorsDatas[vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH + 2] = color;
                a_ColorsDatas[vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH + 3] = color;
                BasicAlgorithm.SetShadowUV3(a_mainChunkVoxel3Ds[newIndex].colorShadows[a_Direction], a_UV3sDatas, vertexPointer * VoxelCategories.UNIT_VERTICES_LENGTH);
                

            }

            if (fm_isAddFill)
            {
                fm_verticesIndex.CopyTo(return_IndexArray_new, 0);
                //添加剩余的
                fm_Difference.CopyTo(return_IndexArray_new, fm_verticesIndex.Length);
                //更新Chunk
                for (int i = 0; i < fm_Difference.Length; i++)
                {
                    a_ChunkArray[fm_Difference[i]] = i + fm_verticesIndex.Length;
                }

                //添加真实数据
                a_VerticesDatas.CopyTo(returnNewArrayVertices, 0);

                a_UVsDatas.CopyTo(returnNewArrayUVs, 0);
                a_UV3sDatas.CopyTo(returnNewArrayUV3s, 0);
                a_ColorsDatas.CopyTo(returnNewArrayColors, 0);

                //添加剩余的
                int loop_StartIndex = fm_intersection.Length;
                for (int i = 0; i < fm_Difference.Length; i++)
                {
                    int indexLength = a_VerticesDatas.Length + i * VoxelCategories.UNIT_VERTICES_LENGTH;
                    var loop_AddChunkIndex = fm_Difference[i];

                    Array.Copy(voxelMetadata3D.GetVertices(a_Direction, a_mainChunkVoxel3Ds[loop_AddChunkIndex].localPosition), 0, returnNewArrayVertices, indexLength, VoxelCategories.UNIT_VERTICES_LENGTH);
                    //Array.Copy(voxelMetadata3D.allDataVertices, lengthDirectionIndex + fm_Difference[i] * 4, returnNewArrayVertices, indexLength, 4);
                    //int loopIndexVertice = lengthDirectionIndex + fm_Difference[i] * 4;
                    //returnNewArrayVertices[indexLength + 0] = voxelMetadata3D.allDataVertices[loopIndexVertice + 0];
                    //returnNewArrayVertices[indexLength + 1] = voxelMetadata3D.allDataVertices[loopIndexVertice + 1];
                    //returnNewArrayVertices[indexLength + 2] = voxelMetadata3D.allDataVertices[loopIndexVertice + 2];
                    //returnNewArrayVertices[indexLength + 3] = voxelMetadata3D.allDataVertices[loopIndexVertice + 3];

                    Array.Copy(voxelMetadata3D.allDataUVs, a_mainChunkVoxel3Ds[loop_AddChunkIndex].uvKey * VoxelCategories.UNIT_VERTICES_LENGTH, returnNewArrayUVs, indexLength, VoxelCategories.UNIT_VERTICES_LENGTH);
                    //int loopIndexUV = a_mainChunkVoxel3Ds[fm_Difference[i]].uvKey * 4;
                    //returnNewArrayUVs[indexLength + 0] = voxelMetadata3D.allDataUVs[loopIndexUV + 0];
                    //returnNewArrayUVs[indexLength + 1] = voxelMetadata3D.allDataUVs[loopIndexUV + 1];
                    //returnNewArrayUVs[indexLength + 2] = voxelMetadata3D.allDataUVs[loopIndexUV + 2];
                    //returnNewArrayUVs[indexLength + 3] = voxelMetadata3D.allDataUVs[loopIndexUV + 3];


                    //Array.Copy(
                    //     voxelMetadata3D.GetColors(a_Direction, a_mainChunkVoxel3Ds[loop_AddChunkIndex].colorShadow[a_Direction])
                    //    , 0, returnNewArrayColors, indexLength, 4);
                    color.a = (a_mainChunkVoxel3Ds[loop_AddChunkIndex].selectKey) / 255f;


                    //var colorShadow = a_mainChunkVoxel3Ds[loop_AddChunkIndex].colorShadow[a_Direction];
                    //BasicAlgorithm.SetShadowColor(a_mainChunkVoxel3Ds[loop_AddChunkIndex].colorShadows[a_Direction], returnNewArrayColors, indexLength, color);
                    returnNewArrayColors[indexLength + 0] = color;
                    returnNewArrayColors[indexLength + 1] = color;
                    returnNewArrayColors[indexLength + 2] = color;
                    returnNewArrayColors[indexLength + 3] = color;

                    BasicAlgorithm.SetShadowUV3(a_mainChunkVoxel3Ds[loop_AddChunkIndex].colorShadows[a_Direction], returnNewArrayUV3s, indexLength);

                }
            }
            else
            {
                //开始删除多余的数据
                //删除的索引集合
                int[] var_DifferenceVerticesData_Index = new int[fm_Difference.Length * VoxelCategories.UNIT_VERTICES_LENGTH];
                for (int i = 0, loop_length = fm_Difference.Length; i < loop_length; i++)
                {
                    int loop_removeIndex = a_ChunkArray[fm_Difference[i]];

                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 0] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 0;
                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 1] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 1;
                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 2] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 2;
                    var_DifferenceVerticesData_Index[i * VoxelCategories.UNIT_VERTICES_LENGTH + 3] = loop_removeIndex * VoxelCategories.UNIT_VERTICES_LENGTH + 3;
                }

                returnNewArrayVertices = RemoveArray_simple(a_VerticesDatas, var_DifferenceVerticesData_Index);
                returnNewArrayUVs = RemoveArray_simple(a_UVsDatas, var_DifferenceVerticesData_Index);
                returnNewArrayUV3s = RemoveArray_simple(a_UV3sDatas, var_DifferenceVerticesData_Index);
                returnNewArrayColors = RemoveArray_simple(a_ColorsDatas, var_DifferenceVerticesData_Index);

                //删除的索引集合
                int[] var_DifferenceVerticesIndex_Index = new int[fm_Difference.Length];
                for (int i = 0, loop_length = fm_Difference.Length; i < loop_length; i++)
                {
                    var_DifferenceVerticesIndex_Index[i] = a_ChunkArray[fm_Difference[i]];
                    a_ChunkArray[fm_Difference[i]] = -1;
                }
                //删除多余的
                return_IndexArray_new = RemoveArray_simple(fm_verticesIndex, var_DifferenceVerticesIndex_Index);

                for (int i = 0; i < return_IndexArray_new.Length; i++)
                {
                    a_ChunkArray[return_IndexArray_new[i]] = i;
                }
            }
            a_VerticesIndex = return_IndexArray_new;
            a_VerticesDatas = returnNewArrayVertices;
            a_UVsDatas = returnNewArrayUVs;
            a_UV3sDatas = returnNewArrayUV3s;
            a_ColorsDatas = returnNewArrayColors;
            //return Tuple.Create(return_IndexArray_new, return_DataArray_new);
        }

        /// <summary>
        /// 删除数组简单版
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="a_array"></param>
        /// <param name="a_RemoveIndexs"></param>
        /// <returns></returns>
        public static T[] RemoveArray_simple<T>(T[] a_array, int[] a_RemoveIndexs)
        {
            int a_RemoveLength = a_RemoveIndexs.Length;
            int[] fm_array_Index = new int[a_array.Length];


            T[] return_array_new = new T[a_array.Length - a_RemoveLength];
            for (int i = 0, loop_Leng = a_RemoveIndexs.Length; i < loop_Leng; i++)
            {
                fm_array_Index[a_RemoveIndexs[i]] = -1;
            }
            for (int i = 0, loop_Length = fm_array_Index.Length, loop_return_index = 0; i < loop_Length; i++)
            {
                if (fm_array_Index[i] != -1)
                {
                    return_array_new[loop_return_index] = a_array[i];
                    loop_return_index++;
                }
            }
            return return_array_new;
        }

        public static T[] RemoveArray<T>(T[] a_array, int[] a_RemoveIndexs)
        {
            List<List<int>> fm_A_PacketSorting_RemoveIndexs = PacketSorting(a_RemoveIndexs);
            int a_RemoveLength = a_RemoveIndexs.Length;


            T[] return_array_new = new T[a_array.Length - a_RemoveLength];

            int var_Grouping_Length = fm_A_PacketSorting_RemoveIndexs.Count;

            int[] var_change_continuityRemoveIndex = new int[var_Grouping_Length * 2 + 2];
            var_change_continuityRemoveIndex[0] = 0;
            var_change_continuityRemoveIndex[1] = fm_A_PacketSorting_RemoveIndexs[0][0];


            for (int i = 1; i < var_Grouping_Length; i++)
            {
                var_change_continuityRemoveIndex[i * 2] = fm_A_PacketSorting_RemoveIndexs[i - 1][0] + fm_A_PacketSorting_RemoveIndexs[i - 1].Count;
                var_change_continuityRemoveIndex[i * 2 + 1] = fm_A_PacketSorting_RemoveIndexs[i][0] - (fm_A_PacketSorting_RemoveIndexs[i - 1][0] + fm_A_PacketSorting_RemoveIndexs[i - 1].Count);
            }

            //var_change_continuityRemoveIndex[2] = a_RemoveIndexs[0][0] + a_RemoveIndexs[0].Length;
            //var_change_continuityRemoveIndex[3] = a_RemoveIndexs[1][0] - (a_RemoveIndexs[0][0] + a_RemoveIndexs[0].Length);

            //var_change_continuityRemoveIndex[4] = a_RemoveIndexs[1][0] + a_RemoveIndexs[1].Length;
            //var_change_continuityRemoveIndex[5] = a_RemoveIndexs[2][0] - (a_RemoveIndexs[1][0] + a_RemoveIndexs[1].Length);



            var_change_continuityRemoveIndex[var_Grouping_Length * 2] = fm_A_PacketSorting_RemoveIndexs[var_Grouping_Length - 1][0] + fm_A_PacketSorting_RemoveIndexs[var_Grouping_Length - 1].Count;
            var_change_continuityRemoveIndex[var_Grouping_Length * 2 + 1] = a_array.Length - (fm_A_PacketSorting_RemoveIndexs[var_Grouping_Length - 1][0] + fm_A_PacketSorting_RemoveIndexs[var_Grouping_Length - 1].Count);

            int loop_return_Index = 0;
            for (int i = 0; i < var_change_continuityRemoveIndex.Length / 2; i++)
            {

                Array.Copy(a_array, var_change_continuityRemoveIndex[i * 2], return_array_new, loop_return_Index, var_change_continuityRemoveIndex[i * 2 + 1]);
                loop_return_Index += var_change_continuityRemoveIndex[i * 2 + 1];
            }

            return return_array_new;
        }


        static List<List<int>> PacketSorting(int[] a_array)
        {
            int[] fm_Max = new int[4096];
            List<List<int>> return_GroupingList;
            if (a_array.Length > 1024)
            {
                return_GroupingList = new List<List<int>>(a_array.Length / 2);
            }
            else
            {
                return_GroupingList = new List<List<int>>(32);
            }

            for (int i = 0; i < a_array.Length; i++)
            {
                fm_Max[a_array[i]] = 1;
            }

            bool loop_addNew = true;
            List<int> loop_Array = new List<int>();
            return_GroupingList.Add(loop_Array);

            for (int i = 0; i < 4096; i++)
            {
                if (fm_Max[i] == 1)
                {
                    loop_Array.Add(i);
                    loop_addNew = true;
                }
                else
                {
                    if (loop_addNew)
                    {
                        loop_Array = new List<int>();
                        return_GroupingList.Add(loop_Array);
                        loop_addNew = false;
                    }
                }

            }

            if (return_GroupingList[0].Count == 0)
            {
                return_GroupingList.RemoveAt(0);
            }
            if (return_GroupingList[return_GroupingList.Count - 1].Count == 0)
            {
                return_GroupingList.RemoveAt(return_GroupingList.Count - 1);
            }
            return return_GroupingList;
        }
    }

}